package MaxInterface;



import com.dukascopy.api.Filter;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.IIndicators.AppliedPrice;



/**
 * @author Barbos
 * Check if conditions for current period and instrument are valid to enter
 * returns:
 * -1 - if no enters
 * 0 - if Buy condition
 * 1 - if Sell condition
 */
public class DashboardSignal {
	
	public DashboardSignal(Instrument instrument, Period period, int param){
		this.instrument = instrument;
		this.period = period;
		this.param = param * instrument.getPipValue();
		envKoef  = new double[][] {{0.13,0.34,0.55,0.89},
								   {0.34,0.55,0.89,1.44},
								   {0.55,0.89,1.44,2.33},
								   {0.55,0.89,1.44,2.33},
								   {0.89,1.44,2.33,3.77}};
		
		if(period == Period.FIVE_MINS){
			envFirstDimmension = 0;
		}
		else if(period == Period.FIFTEEN_MINS){
			envFirstDimmension = 1;
		}
		else if(period == Period.THIRTY_MINS){
			envFirstDimmension = 2;
		}
		else if(period == Period.ONE_HOUR){
			envFirstDimmension = 3;
		}
		else if(period == Period.FOUR_HOURS){
			envFirstDimmension = 4;
		}
		
	}
	
	public int writeInfo(){
		return entryType;
	}
	
	public int Signal (IBar bid, IIndicators indicators) throws JFException{
		
		double[] ma21 = indicators.ema(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 21, Filter.WEEKENDS, 4, bid.getTime(), 0);
		double[] ma55 = indicators.ema(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 55, Filter.WEEKENDS, 6, bid.getTime(), 0);
		double[] ma89 = indicators.ema(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 89, Filter.WEEKENDS, 9, bid.getTime(), 0);
		double[] ma233 = indicators.ema(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 233, Filter.WEEKENDS, 2, bid.getTime(), 0);
		/*1 - up
		0 - down*/ 
		double[][] env1 = indicators.emaEnvelope(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 233, envKoef[envFirstDimmension][0], Filter.WEEKENDS, 2, bid.getTime(), 0);
		double[][] env2 = indicators.emaEnvelope(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 233, envKoef[envFirstDimmension][1], Filter.WEEKENDS, 2, bid.getTime(), 0);
		double[][] env3 = indicators.emaEnvelope(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 233, envKoef[envFirstDimmension][2], Filter.WEEKENDS, 2, bid.getTime(), 0);
		double[][] env4 = indicators.emaEnvelope(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 233, envKoef[envFirstDimmension][3], Filter.WEEKENDS, 2, bid.getTime(), 0);
		
		if((bid.getClose() > ma21[0]) && ((bid.getClose() - ma21[0]) <= param) && (ma21[0] > ma55[0]) && (ma55[0] > ma89[0]) && (ma89[0] > ma233[0]) && (bid.getClose() < env3[1][0])){
			entryType = 21;
			posType = 0;
		}
		else if((bid.getClose() < ma21[0]) && ((ma21[0] - bid.getClose()) <= param) && (ma21[0] < ma55[0]) && (ma55[0] < ma89[0]) && (ma89[0] < ma233[0]) && (bid.getClose() > env3[0][0])){
			entryType = 21;
			posType = 1;
		}
		else if((bid.getClose() > ma21[0]) && (bid.getClose() > ma233[0]) && ((bid.getClose() - ma233[0]) <= param) && (ma21[0] > ma55[0]) && (ma55[0] > ma89[0]) && (bid.getClose() < env2[1][0])){
			entryType = 233;
			posType = 0;
		}
		else if((bid.getClose() < ma21[0]) && (bid.getClose() < ma233[0]) && ((ma233[0] - bid.getClose()) <= param) && (ma21[0] < ma55[0]) && (ma55[0] < ma89[0]) && (bid.getClose() > env2[0][0])){
			entryType = 233;
			posType = 1;
		}
		
		if(posType >= 0){
			this.ma21 = ma21[0];
			this.ma55 = ma55[0];
			this.ma89 = ma89[0];
			this.ma233 = ma233[0];
			this.oprice = bid.getClose();
		}
		
		return posType;
	}
	
	public void CalculateFan(){
		d_fanOrder[0] = oprice;
		d_fanOrder[1] = ma21;
		d_fanOrder[2] = ma55;
		d_fanOrder[3] = ma89;
		d_fanOrder[4] = ma233;
		
		s_fanOrder[0] = "P";
		s_fanOrder[1] = "21";
		s_fanOrder[2] = "55";
		s_fanOrder[3] = "89";
		s_fanOrder[4] = "233";
		
		int j = 1;
		double dt = 0;
		String st = "";
		
		while(j != 0){
			j = 0;
			for(int i = 0; i < (d_fanOrder.length-1); i++){
				if(d_fanOrder[i] > d_fanOrder[i+1]){
					dt = d_fanOrder[i+1];
					d_fanOrder[i+1] = d_fanOrder[i];
					d_fanOrder[i] = dt;
					
					st = s_fanOrder[i+1];
					s_fanOrder[i+1] = s_fanOrder[i];
					s_fanOrder[i] = st;
					j++;
				}
			}
		}
		
	}
	
	public String[] getFanOrder(){
		return s_fanOrder;
	}
	
	public double[] getFanValues(){
		return d_fanOrder;
	}
	
	public String getJDialogName(){
		if(posType == 0) 
			jDialogName = instrument.toString() + "Buy signal";
		else if(posType == 1) 
			jDialogName = instrument.toString() + "Sell signal";
		
		return jDialogName; 
	}
	
	
	double[][] t = null;
	private Instrument instrument = null;
	private Period period = null;
	private double param = 0;
	private double [][]envKoef = null;
	private int envFirstDimmension = 0;
	private int entryType = 0;
	private int posType = -1;
	private double ma21 = 0,
				   ma55 = 0,
				   ma89 = 0,
				   ma233 = 0,
				   oprice = 0;
	private double [] fanDistance = new double[4];
	private double [] d_fanOrder = new double[5];
	private String [] s_fanOrder = new String[5];
	
	private String jDialogName = ""; 
	
	
}

